﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GestionePolveriLib.BL;
using GestionePolveriLib.Facades;

namespace GestionePolveri2
{
    public partial class POSUpdateDialog : Form
    {
        private POS m_posValue = null;
        private bool m_close = false;

        public POS PosValue
        {
            get { return m_posValue; }
            set 
            { 
                m_posValue = value;

                if (m_posValue != null)
                {
                    this.lblDataPOS.Text = string.Format("Data: {0}", m_posValue.DateOfPos.ToLongDateString());
                    this.lblImpianto.Text = string.Format("Impianto: {0}", m_posValue.Impianto);
                    this.lblTurno.Text = string.Format("Turno: {0}", m_posValue.Turno);
                    this.lblOperatore.Text = string.Format("Operatore: {0}", m_posValue.Operatore);
                    this.textBox1.Text = m_posValue.POSValue.ToString();
                }
            
            }
        }

        public POSUpdateDialog()
        {
            InitializeComponent();

            this.FormClosing += new FormClosingEventHandler(POSUpdateDialog_FormClosing);
        }

        void POSUpdateDialog_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = m_close;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                m_posValue.POSValue = int.Parse(this.textBox1.Text);

                POSTableFacade ptf = new POSTableFacade();
                bool ret = ptf.Modify(m_posValue);

                if (ret)
                {
                    m_close = false;
                    MessageBox.Show("Valore aggiornato !", "Gestione Polveri", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                }
            }
            catch (Exception ex)
            {
                m_close = true;
                MessageBox.Show("Sono accettati solo valori numerici interi. Inserire un valore corretto.", "Gestione Polveri", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
    }
}
